<template>
  <div>
    <Xdh-table ref="dragTable" :columns="tablefields" :data="formData.domains">
      <div slot="search">
        <el-card shadow="never">
          <div slot="header" class="clearfix">
            <span>搜索</span>
          </div>
          <div class="text item">
            <xdh-form :model="model" :fields="fields" @submit="handleSubmit" :edits="true"></xdh-form>
          </div>
        </el-card>
      </div>

      <div slot="leftButton" class="left-button" style="height:50px;border:1px solid #FFFFFF">
        <div v-if="leftButtonField.length">
          <template v-for="(item,index) in leftButtonField">
            <div class="mask" :key="index">
              <XdhTableItem v-bind="item" :type="item.type" :label="item.label" :prop="item.prop" />
            </div>
          </template>
        </div>
        <p v-else style="text-align:center;line-height:20px">左边按钮拖拽区域</p>
      </div>

      <div slot="rightButton" class="left-button" style="height:50px;border:1px solid #FFFFFF">
        <div v-if="rightButtonField.length">
          <template v-for="(item,index) in rightButtonField">
            <div class="mask" :key="index">
              <XdhTableItem v-bind="item" :type="item.type" :label="item.label" :prop="item.prop" />
            </div>
          </template>
        </div>
        <p v-else style="text-align:center;line-height:20px">左边按钮拖拽区域</p>
      </div>

    </Xdh-table>

  </div>
</template>
<style lang="stylus" rel="stylesheet/stylus" scoped type="text/stylus">
.node {
  position: relative;
  font-size: 16px;
  padding: 5px;
  padding-top: 10px;

  &::before {
    content: '';
    display: block;
    overflow: hidden;
    height: 0;
    width: 1px;
  }
}
</style>
<script type="text/ecmascript-6">
import XdhForm from '@/widgets/xdh-form'
import XdhTable from '@/widgets/xdh-table'
import XdhFormItem from '@/widgets/xdh-form/xdh-form-item'
import FormMixin from '@/base/mixin/forms'
import cloneDeep from 'lodash/cloneDeep'
import Vue from 'vue'
import { getForms } from '@/widgets/helper/storage'
import XdhTableItem from '@/widgets/xdh-table/xdh-table-item'
import { mapState, mapGetters } from 'vuex'

export default {
  name: 'tablePrenode',
  components: {
    XdhForm, XdhFormItem, XdhTable, XdhTableItem
  },
  mixins: [FormMixin],
  props: {
    scope: {
      type: Object
    },
    stacked: {
      type: Boolean,
      default: true
    },
    info: {
      required: true,
      type: Array
    },
    tableinfo: {
      required: true,
      type: Array
    },
    slotStyle: {
      type: Object,
      default: null
    }
  },
  data: function () {
    return {
      oldId: '',
      tab_id: '0',
      isActive: false,
      currPage: '',
      oldStyle: {},
      nodeInfo: (() => {
        if (this.scope) {
          return cloneDeep(this.info)
        } else {
          console.log(this.info, 'info================================')
          return this.info[0]['content']
        }
      })(),
      fields: [],
      tablefields: [],
      load: null,
      formData: {
        domains: [{ id: 1, title: "你好中国人" }, { id: 2, title: "你好美国人" }]
      },
    }
  },
  computed: {
    ...mapState({
      leftButtonField: state => state.leftButtonField,
      rightButtonField: state => state.rightButtonField,

    }),
    model() {
      const m = {}
      this.fields.forEach(item => {
        if (!item) return
        // 分隔线组件无值
        if (item.type !== 'divider') {
          m[item.prop] = (item.props || {}).value || item.value
        }
      })
      return m
    },
    tableModel() {
      const m = {}
      this.tablefields.forEach(item => {
        if (!item) return
        // 分隔线组件无值
        if (item.type !== 'divider') {
          m[item.prop] = (item.props || {}).value || item.value
        }
      })
      return m
    }
  },
  watch: {
    info: {
      deep: true,
      handler: function (newVal) {
        console.log(newVal, 'info================================')
        var info = {}
        if (this.scope) {
          info = cloneDeep(newVal)
        } else {
          info = newVal
        }
        this.nodeInfo = info
      }
    },
  },
  created() {
    this.fields = this.nodeInfo || []
    this.tablefields = this.tableinfo || []
  },
  mounted() {

  },
  methods: {
    clone(data, row) {
      var result = this._.cloneDeep(data);
      result.props['value'] = row[data.prop];
      console.log('clone', data, row[data.prop], result)
      return result
    },
    handleSubmit(model) {
      this.$message({
        message: '若要保存页面，请点击头部的保存按钮',
        type: 'success'
      });
      console.log('submit', model, this.fields)
    },
    cloneDeep: cloneDeep,
  }
}
</script>

<style>
.mask {
  float: left;
  position: relative;
  min-width: 70px;
  min-height: 40px;
  margin-left: 5px;
}
.mask .del {
  position: absolute;
  top: 0px;
  right: 0px;
}
</style>
